#include"dlinklist.h"

dlinklist init_list(void){
    dlinklist head = malloc(sizeof(dlistnode));
    if(head != NULL){
        head->prev = head;
        head->next = head;
    }
    return head;
}

dlinklist new_node(int data){
    dlinklist new_node = malloc(sizeof(dlistnode));
    if(new_node != NULL){
        new_node->data = data;
        new_node->prev = new_node;
        new_node->next = new_node;
    }
    return new_node;
}

void dlist_add_tail(dlinklist head, dlinklist new_node){
    new_node->prev = head->prev;
    new_node->next = head;

    head->prev->next = new_node;
    head->prev = new_node;
}

void del_node(dlinklist p){
    p->prev->next = p->next;
    p->next->prev = p->prev;

    p->prev = p;
    p->next = p;
}

void dlist_for_each(dlinklist head){
    if(head == NULL)
        return;
    dlinklist p = head->next;
    while(p != head){
        printf("%d\t", p->data);
        p = p->next;
    }
    printf("\n");
}